package com.example.loader;

import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.res.AssetManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.loader.utils.Logger;
import com.example.loader.view.PageLoader;

public class MainActivity extends Activity {

	protected static final String TAG = "MainActivity";
	private LinearLayout ll_main_loding;
	private TextView tv_main_content;
	
	private PageLoader loader_main_layout;
	private LoadContentTask loadContentTask;
	
	private static String STATE = "";
	private static String CURRENT_CHAPTER = "";
	
	private static String NO_PRE="没有上一章了";
	private static String NO_NEXT="没有下一章了";
	private TextView tv_main_header;
	private TextView tv_main_footer;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initView();
		loadContentTask = new LoadContentTask();
		loadContentTask.execute(STATE);
	}

	/**
	 * 初始化布局
	 */
	private void initView() {
		loader_main_layout = (PageLoader) findViewById(R.id.loader_main_layout);
		ll_main_loding = (LinearLayout) findViewById(R.id.ll_main_loding);
		tv_main_content = (TextView) findViewById(R.id.tv_main_content);
		
		tv_main_header = (TextView) findViewById(R.id.tv_main_header);
		tv_main_footer = (TextView) findViewById(R.id.tv_main_footer);
		tv_main_header.setHeight(0);
		tv_main_footer.setHeight(0);
		
		// 给PageLoader设置滑动监听
		LoaderListenerImp listener = new LoaderListenerImp();
		loader_main_layout.setOnScrollListener(listener);
	}

	@Override
	protected void onResume() {
		super.onResume();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
	}


	class LoadContentTask extends AsyncTask<String, Object, String> {

		private AssetManager manager;

		@Override
		protected void onPreExecute() {

			tv_main_content.setVisibility(View.INVISIBLE);
			ll_main_loding.setVisibility(View.VISIBLE);

			tv_main_header.setVisibility(View.INVISIBLE);
			tv_main_footer.setVisibility(View.INVISIBLE);
			loader_main_layout.setVisibility(View.GONE);
			
			manager = getAssets();
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(String... params) {
			SystemClock.sleep(3000);
			StringBuilder sb = new StringBuilder();
			String action = params[0];
			if ("".equalsIgnoreCase(CURRENT_CHAPTER)
					&& "".equalsIgnoreCase(action)) {
//				Logger.d(TAG, "---文件名为空，加载第一章---");
				CURRENT_CHAPTER = "1.txt";
			} else if ("pre".equalsIgnoreCase(action)) {
				if (CURRENT_CHAPTER.equalsIgnoreCase("1.txt")) {
//					Logger.d(TAG, "---没有上一章了---");
					return NO_PRE;
				} else {
//					Logger.d(TAG, "---加载上一章---");
					CURRENT_CHAPTER = "1.txt";
				}
			} else if ("next".equalsIgnoreCase(action)) {
				if (CURRENT_CHAPTER.equalsIgnoreCase("2.txt")) {
//					Logger.d(TAG, "---没有下一章了---");
					return NO_NEXT;
				} else {
//					Logger.d(TAG, "---加载下一章---");
					CURRENT_CHAPTER = "2.txt";
				}
			}
			InputStream inputStream = null;
			try {
				inputStream = manager.open(CURRENT_CHAPTER);
				byte[] buf = new byte[1024];
				int len = -1;
				while ((len = inputStream.read(buf)) != -1) {
					sb.append(new String(buf, 0, len));
				}
			} catch (IOException e) {
				new IOException("文件读取错误");
			} finally {
				try {
					inputStream.close();
				} catch (IOException e) {
					new IOException("文件关闭错误");
				}
			}
			return sb.toString();
		}

		@Override
		protected void onPostExecute(String result) {
			if (NO_PRE.equalsIgnoreCase(result)) {
				Toast.makeText(getApplicationContext(), NO_PRE, Toast.LENGTH_LONG).show();
			} else if (NO_NEXT.equalsIgnoreCase(result)) {
				Toast.makeText(getApplicationContext(), NO_NEXT, Toast.LENGTH_LONG).show();
				loader_main_layout.scrollToBottom();
			} else {
				tv_main_content.setText(result);
				loader_main_layout.scrollToTop();
			}
			tv_main_content.setVisibility(View.VISIBLE);
			ll_main_loding.setVisibility(View.INVISIBLE);
			
			loader_main_layout.setVisibility(View.VISIBLE);
			tv_main_header.setVisibility(View.VISIBLE);
			tv_main_footer.setVisibility(View.VISIBLE);
			super.onPostExecute(result);
		}

	}

	class LoaderListenerImp implements PageLoader.LoaderScrollListener {

		@Override
		public void preChapter() {
			loadPre();
		}

		@Override
		public void nextChapter() {
			loadNext();
		}

		@Override
		public int setHeaderHeight(float dy) {
			tv_main_header.setHeight((int) dy);
			tv_main_header.invalidate();
			return tv_main_header.getHeight();
		}

		@Override
		public int setFooterHeight(float dy) {
			tv_main_footer.setHeight((int) dy);
			tv_main_footer.invalidate();
			return tv_main_footer.getHeight();
		}


	}

	/**
	 * 加载上一章
	 */
	public void loadPre() {
		STATE = "pre";
		if (!loadContentTask.isCancelled()) {
			loadContentTask.cancel(true);
			loadContentTask = new LoadContentTask();
			loadContentTask.execute(STATE);
		}
	}

	/**
	 * 加载下一章
	 */
	public void loadNext() {
		STATE = "next";
		if (!loadContentTask.isCancelled()) {
			loadContentTask.cancel(true);
			loadContentTask = new LoadContentTask();
			loadContentTask.execute(STATE);
		}
	}
}
